package com.go.where.manager.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.MessageDao;
import com.go.where.manager.dao.MsgCustomerRelationDao;
import com.go.where.manager.exception.ManagerException;
import com.go.where.manager.jerseyapi.EasemobMessages;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.MessageInfo;
import com.go.where.manager.pojo.MsgCustomerRelation;
import com.go.where.manager.service.MessageService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.PropertiesUtils;
import com.go.where.manager.vo.EasemobVo;

/**
 * 消息服务
 * 
 * @author
 * 
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

	protected static Logger log = Logger.getLogger(MessageServiceImpl.class);
	@Resource(name = "messageDao")
	private MessageDao messageDao;
	@Resource(name = "msgCustomerRelationDao")
	private MsgCustomerRelationDao msgCustomerRelationDao;

	@Override
	public String saveMessage(MessageInfo messageInfo,
			List<String> customerIds, String sendCustomerType) {
		Date sendTime = new Date();
		EasemobVo vo = new EasemobVo();
		// 先发消息
		vo.setMessageType(Constants.MESSAGE_TYPE_SYSTEM_MESSAGE);
		vo.setMessageContent(messageInfo.getMessageContent());
		vo.setMessageTitle(messageInfo.getMessageTitle());
		vo.setMessageTime(sendTime);
		String systemAdmin = PropertiesUtils.getProperties().getProperty(
				"APP_ADMIN_ACCOUNT");
		List<String> result = null;
		try {
			// 个人用户或邀请码用户
			if ((Constants.PERSONAL_USERS.equals(sendCustomerType) || Constants.INVITATION_USERS
					.equals(sendCustomerType))) {
				result = EasemobMessages.sendMessages(systemAdmin, customerIds,
						CommonUtil.gson().toJson(vo));
			} else if (Constants.ALL_USERS.equals(sendCustomerType)) { // 所有用户
				// customerIds = customerDao.queryCustomerIds(params);
				result = EasemobMessages.sendMessages(systemAdmin, customerIds,
						CommonUtil.gson().toJson(vo));
			}
		} catch (Exception e) {
			log.error("消息推送失败", e);
			throw new ManagerException("消息推送失败");
		}

		String message = "";
		if (result == null || result.isEmpty()) {
			this.save(messageInfo, customerIds);
		} else {
			for (String cid : result) {
				message += cid + "<br/>";
			}
		}
		return message;
	}

	private int save(MessageInfo messageInfo, List<String> customerIds) {
		messageInfo.setMessageId(UUID.randomUUID().toString());
		messageInfo.setIsDelete(Constants.NOT_DELETED);
		List<MsgCustomerRelation> relations = new ArrayList<MsgCustomerRelation>();
		if (CollectionUtils.isNotEmpty(customerIds)) {
			for (String customerId : customerIds) {
				MsgCustomerRelation relation = new MsgCustomerRelation();
				relation.setCustomerId(customerId);
				relation.setMessageId(messageInfo.getMessageId());
				relations.add(relation);
			}
			msgCustomerRelationDao.saveMsgCustomers(relations);
		}
		messageInfo.setSendTime(new Date());
		int result = messageDao.insert(messageInfo);
		return result;
	}

	@Override
	public Pagination<MessageInfo> queryPage(MessageInfo messageInfo) {
		Integer count = messageDao.totalCount(messageInfo);
		messageInfo.calculate();
		List<MessageInfo> list = messageDao.queryList(messageInfo);
		if (list != null && !list.isEmpty()) {
			for (MessageInfo message : list) {
				message.setTargetCustomer(msgCustomerRelationDao
						.queryRelations(message.getMessageId()));
			}
		}
		Pagination<MessageInfo> page = messageDao.queryPagination(count, list,
				messageInfo);

		return page;
	}

	@Override
	public int update(MessageInfo messageInfo) {
		return messageDao.update(messageInfo);
	}

}
